home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / WDEF_FOL / PALETTE.C
Text File  |  1991-12-17  |  7KB  |  269 lines

  1. /*****************************************************************
  2.  "Palette.c"
  3.  
  4.  from the pioneering work by:
  5.  
  6.      Don Melton & Mike Ritter    [MacTutor, April, 1988]
  7.      
  8.  as adapted by:
  9.  
  10.      John A. Love, III            [Washington Apple Pi Users' Group]
  11.  
  12.  using Symantec's "THINK C", v 5.00
  13.  *****************************************************************/
  14.  
  15.  
  16.  
  17.  
  18. // Prototypes:
  19.  
  20. pascal long        main (short variation, WindowPtr whichWindow, short message,
  21.                       long parameter);
  22. void            DrawWindow (WindowPtr whichWindow, long parameter);
  23. long            TestWindowHit (WindowPtr whichWindow, long parameter);
  24. void            CalcWindowRegions (short variation, WindowPtr whichWindow);
  25.  
  26.  
  27. typedef    struct    QuickDraw    {
  28. /* When allocated on the Stack,
  29. ** stored in reverse order:        */
  30.  
  31.     char        private[76];        // low memory.
  32.     long        randSeed;
  33.     BitMap        screenBits;
  34.     Cursor        arrow;
  35.     Pattern        dkGray, ltGray, gray, black, white;
  36.     GrafPtr        thePort;            // High memory.
  37. }    QuickDraw;
  38.  
  39.  
  40. #define        PALETTE_TITLE_BAR_HEIGHT    10
  41. #define        PALETTE_SHADOW_INDENT        2
  42.  
  43.  
  44.  
  45. pascal long        main (short variation, WindowPtr whichWindow, short message,
  46.                       long parameter)    {
  47.  
  48.         long    result = 0;
  49.         
  50.         
  51.     if (message == wDraw)            DrawWindow(whichWindow, parameter);
  52.     else if (message == wHit)        result = TestWindowHit(whichWindow, parameter);
  53.     else if (message == wCalcRgns)    CalcWindowRegions(variation, whichWindow);
  54.     
  55.     /* Ignore wNew & wDispose because we are using standard type windows.
  56.     ** Ignore wGrow & wDrawGIcon because we are NOT using growable windows. */
  57.     
  58.     return (result);
  59.     
  60. }    /* main */
  61.  
  62.  
  63.  
  64. void    DrawWindow (WindowPtr whichWindow, long parameter)        {
  65.  
  66.         WindowPeek        whichPWindow;
  67.         Boolean            hasShadowFrame, hasTitleBar;
  68.         Rect            frame, goAway;
  69.         PenState        savePen;
  70.         short            tone, index;
  71.         Pattern            hilite;
  72.         QuickDraw        *qdGlobals;
  73.         
  74.         
  75.     whichPWindow = (WindowPeek) whichWindow;
  76.  
  77.     if (whichPWindow->visible)    {
  78.     
  79.         // Setup a pointer to QuickDraw's Globals:
  80.         qdGlobals = (QuickDraw*)(*(Ptr*)CurrentA5 /* Ptr to thePort */ -
  81.                                  (sizeof(QuickDraw) - sizeof(GrafPtr)) );
  82.     
  83.         hasShadowFrame = (*whichPWindow->strucRgn)->rgnBBox.right >
  84.                          (*whichPWindow->contRgn )->rgnBBox.right + 1;
  85.         hasTitleBar    = (*whichPWindow->contRgn )->rgnBBox.top ==
  86.                          (*whichPWindow->strucRgn)->rgnBBox.top + 1 +
  87.                                                     PALETTE_TITLE_BAR_HEIGHT;
  88.     
  89.         // Calculate the window frame:
  90.         
  91.         frame = (*whichPWindow->strucRgn)->rgnBBox;
  92.         
  93.         // Calculate the goAway box.  We'll determine later IF it has one:
  94.         
  95.         goAway = frame;
  96.         goAway.top += 2;
  97.         goAway.left += 8;
  98.         goAway.bottom = goAway.top + 7;
  99.         goAway.right = goAway.left + 7;
  100.         
  101.         if (parameter)        InvertRect(&goAway);    /* = wInGoAway */
  102.         else    {
  103.             // Draw window frame, drop shadow, title bar & goAway box:
  104.             
  105.             GetPenState(&savePen);
  106.             PenNormal();
  107.             ;
  108.             if (hasShadowFrame)    {
  109.                 frame.bottom -= 1;
  110.                 frame.right  -= 1;
  111.             }
  112.             FrameRect(&frame);
  113.             
  114.             if (hasShadowFrame)    {
  115.                 MoveTo(frame.left + PALETTE_SHADOW_INDENT, frame.bottom);
  116.                 LineTo(frame.right, frame.bottom);
  117.                 LineTo(frame.right, frame.top + PALETTE_SHADOW_INDENT);
  118.             }
  119.             
  120.             if (hasTitleBar)    {
  121.                 frame.bottom = frame.top + (PALETTE_TITLE_BAR_HEIGHT + 1);
  122.                 FrameRect(&frame);
  123.                 InsetRect(&frame, 1, 1);
  124.             
  125.                 if (whichPWindow->hilited)    {
  126.             
  127.                     // Adjust pattern and fill title bar:
  128.                 
  129.                     tone = (frame.left & 1)    ? 0x55 : 0xAA;
  130.                 
  131.                     for (index = 0; index <= 7; index++)
  132.                         hilite[index] = ((index + frame.top) & 1) ? tone : 0;
  133.                     
  134.                     FillRect(&frame, &hilite);
  135.                 
  136.                     // Draw goAway box:
  137.                 
  138.                     if (whichPWindow->goAwayFlag)    {
  139.                         frame = goAway;
  140.                         InsetRect(&frame, -1, -1);
  141.                         EraseRect(&frame);
  142.                         FrameRect(&goAway);
  143.                     }
  144.                 
  145.                 }    /* if hilited */
  146.                 else    FillRect(&frame, qdGlobals->white);
  147.             }    /* has a Title Bar */
  148.             ;
  149.             SetPenState(&savePen);
  150.             
  151.         }    /* if parameter == 0 */
  152.         
  153.     }    /* if window is visible */
  154.     
  155. }    /* DrawWindow */
  156.  
  157.  
  158.  
  159. long    TestWindowHit (WindowPtr whichWindow, long parameter)    {
  160.  
  161.         WindowPeek        whichPWindow;
  162.         Boolean            hasShadowFrame, hasTitleBar;
  163.         Point            where;
  164.         Rect            drag, goAway;
  165.         
  166.         
  167.     whichPWindow = (WindowPeek) whichWindow;
  168.     hasShadowFrame = (*whichPWindow->strucRgn)->rgnBBox.right >
  169.                      (*whichPWindow->contRgn )->rgnBBox.right + 1;
  170.     hasTitleBar    = (*whichPWindow->contRgn )->rgnBBox.top ==
  171.                      (*whichPWindow->strucRgn)->rgnBBox.top + 1 +
  172.                                                 PALETTE_TITLE_BAR_HEIGHT;
  173.     
  174.     where.v = HiWord(parameter);
  175.     where.h = LoWord(parameter);
  176.     
  177.     if ( PtInRect(where, &(*whichPWindow->contRgn)->rgnBBox) )
  178.         return (wInContent);
  179.         
  180.     else if (hasTitleBar)    {
  181.         // Mouse in title bar somehere:
  182.         
  183.         drag = (*whichPWindow->strucRgn)->rgnBBox;
  184.         InsetRect(&drag, 1, 1);
  185.         if (hasShadowFrame)        drag.right -= 1;
  186.         drag.bottom = drag.top + (PALETTE_TITLE_BAR_HEIGHT - 1);
  187.         
  188.         if (PtInRect(where, & drag))    {
  189.         
  190.             if (whichPWindow->goAwayFlag)    {
  191.                 goAway = drag;
  192.                 goAway.top += 1;
  193.                 goAway.left += 7;
  194.                 goAway.bottom = goAway.top + 7;
  195.                 goAway.right = goAway.left + 7;
  196.             
  197.                 if ( whichPWindow->hilited && PtInRect(where, &goAway) )
  198.                         return (wInGoAway);
  199.                 else    return (wInDrag);
  200.             }
  201.             else        return (wInDrag);
  202.             
  203.         }
  204.         
  205.     }    /* Mouse in title bar somehere */
  206.     
  207.     return (wNoHit);
  208.     
  209. }    /* TestWindowHit */
  210.  
  211.  
  212.  
  213. void    CalcWindowRegions (short variation, WindowPtr whichWindow)    {
  214.  
  215.         WindowPeek        whichPWindow;
  216.         Boolean            hasShadowFrame, hasTitleBar;
  217.         Rect            windowRect;
  218.         RgnHandle        shadowRgn;
  219.         
  220.         
  221.     whichPWindow = (WindowPeek) whichWindow;
  222.     hasShadowFrame = (variation == documentProc ) ||
  223.                      (variation == noGrowDocProc) ||
  224.                      (variation == altDBoxProc);
  225.     hasTitleBar    = (variation == documentProc ) ||
  226.                      (variation == noGrowDocProc) ||
  227.                      (variation == rDocProc);
  228.     
  229.     // Calculate content region of the window:
  230.     
  231.     windowRect = whichWindow->portRect;
  232.     /* The window's BitMap, portBits, includes the Title Bar.
  233.     ** So, we first expand the portRect and later bring it
  234.     ** back to compute just the contRgn.                    */
  235.     if (hasTitleBar)    windowRect.top -= PALETTE_TITLE_BAR_HEIGHT;
  236.     OffsetRect(&windowRect,            /* LocalToGlobal */
  237.                -whichWindow->portBits.bounds.left,
  238.                -whichWindow->portBits.bounds.top);
  239.     if (hasTitleBar)    windowRect.top += PALETTE_TITLE_BAR_HEIGHT;
  240.     RectRgn(whichPWindow->contRgn, &windowRect);
  241.     
  242.     /* Calculate structure region of the window:
  243.        a) frame   b) title bar   c) shadow of frame */
  244.     
  245.     InsetRect(&windowRect, -1, -1);
  246.     RectRgn(whichPWindow->strucRgn, &windowRect);
  247.     
  248.     if (hasTitleBar)    {
  249.         windowRect.top -= PALETTE_TITLE_BAR_HEIGHT;
  250.         RectRgn(whichPWindow->strucRgn, &windowRect);
  251.     }
  252.     
  253.     if (hasShadowFrame)    {
  254.         windowRect.top    += PALETTE_SHADOW_INDENT;
  255.         windowRect.left   += PALETTE_SHADOW_INDENT;
  256.         windowRect.bottom += 1;        /* Width of the shadow = 1 */
  257.         windowRect.right  += 1;
  258.         RectRgn(shadowRgn = NewRgn(), &windowRect);
  259.         UnionRgn(whichPWindow->strucRgn, shadowRgn, whichPWindow->strucRgn);
  260.         DisposeRgn(shadowRgn);
  261.     }
  262.  
  263. }    /* CalcWindowRegions */
  264.  
  265.  
  266.  
  267.  
  268. /*    { end file "Palette.c" }  */
  269.